package net.crowdloud.shared.engine;

import java.util.ArrayList;
import java.util.List;

import net.crowdloud.shared.entities.Problem;
import net.crowdloud.shared.entities.Solution;
import net.crowdloud.shared.entities.User;

public class CrowdLoudProviderEngine {

	public static List<User> getUsersProviders(List<User> allUsers,
			User userCustomer) {

		List<User> usersProviders = new ArrayList<User>();

		List<String> problemsTags = new ArrayList<String>();

		for (Problem problem : userCustomer.getUserProblems()) {

			if (problem.getTags() != null) {

				for (String problemTag : problem.getTags().split(",")) {

					problemsTags.add(problemTag);
				}
			}
		}

		for (User user : allUsers) {

			boolean foundProvider = false;

			for (Solution providerSolution : user.getUserSolutions()) {

				if (providerSolution.getTags() != null) {

					for (String providerSolutionTag : providerSolution
							.getTags().split(",")) {

						for (String problemTag : problemsTags) {

							if (problemTag != null && problemTag.length() > 0
									&& problemTag.equals(providerSolutionTag)) {

								usersProviders.add(user);

								foundProvider = true;

								break;
							}
						}

						if (foundProvider) {

							break;
						}
					}

					if (foundProvider) {

						break;
					}
				}
			}
		}

		return usersProviders;
	}

}
